package com.neudeu.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neudeu.dao.GoodsDAO;
import com.neudeu.dao.ICategoryDAO;
import com.neudeu.dao.IUserDAO;
import com.neudeu.dto.GoodsInsert;
import com.neudeu.dto.GoodsResult;
import com.neudeu.dto.UserLogin;
import com.neudeu.entity.CategoryEntity;
import com.neudeu.entity.GoodsEntity;
import com.neudeu.entity.UserEntity;
import com.neudeu.page.Page;
import com.neudeu.service.CategoryService;
import com.neudeu.service.GoodsService;
import com.neudeu.service.UserService;
import com.neudeu.util.ImgUtil;

@Service
public class GoodsServiceImpl implements GoodsService {
     
	@Autowired
	private GoodsDAO goodsdao;
	@Autowired
	private ICategoryDAO catedao;

	public List<GoodsResult> all() {
		List<GoodsEntity> goods=goodsdao.getAll();
		List<GoodsResult> results=new ArrayList<GoodsResult>();
		for (GoodsEntity g : goods) {
			GoodsResult gr=new GoodsResult();
			gr.setGid(g.getGid());
			gr.setGname(g.getGname());
			gr.setCname(catedao.getOne(g.getCid()).getCname());
			gr.setImgpath(g.getImgpath());
			results.add(gr);
		}
		return results;
	}

	public Integer modify(GoodsInsert goods,String path) {
		
		GoodsEntity good=new GoodsEntity();
		good.setCid(goods.getCid());
		good.setGname(goods.getGname());
		try {
			good.setImgpath(ImgUtil.getImgPath(goods.getImgfile(), path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return goodsdao.insert(good);
	}

	
	
	
}
